export function isLicensePlate(str) {
    // eslint-disable-next-line max-len
    return /^(([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z](([0-9]{5}[DF])|([DF]([A-HJ-NP-Z0-9])[0-9]{4})))|([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z][A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳使领]))$/.test(
        str
    );
}
export const validatNumber = name => (rule, value, callback) => {
    const reg = /^(([1-9]{1}\d*)|(0{1}))(\.\d{0,3})?$/;
    console.log('value1', value);
    value = (value || '0').toString();
    if (value && value.slice(0, 1) === '-') {
        value = value.slice(1);
    }

    if (!value || (value && reg.test(value))) {
        callback();
    } else {
        return callback(new Error(name + '应为0-3位小数的数字'));
    }
};

const validatInt = name => (rule, value, callback) => {
    const reg = /^[0-9]+?$/;
    if ((value !== 0 && value && !reg.test(value)) || (value + '').includes('.')) {
        return callback(new Error(name + '必须为正整数'));
    }

    callback();
};

export const validatString = count => (rule, value, callback) => {
    value = (value || '').toString();

    if (value.length >= 0 && value.length <= count) {
        callback();
    } else {
        return callback(new Error(name + `长度限制为${count}个字符`));
    }
};

export default function getRules() {
    const RULES = {
        licensePlate: [
            {required: true, message: '车牌号必填项', trigger: ['change', 'blur']},
            {
                validator: (rule, value, callback) => {
                    if (!isLicensePlate(value)) {
                        callback(new Error('车牌号码格式不正确'));
                        return;
                    }
                    callback();
                },
                trigger: ['change', 'blur']
            }
        ],
        brandType: [
            {required: true, message: '品牌型号必填项', trigger: ['change', 'blur']},
            {validator: validatString(30), trigger: ['change', 'blur']}
        ],
        driver: [{required: true, message: '驾驶员必填项', trigger: ['change', 'blur']}],
        price: [{validator: validatNumber('车辆价格'), trigger: ['change', 'blur']}],
        bodyColor: [{validator: validatString(30), trigger: ['change', 'blur']}],
        mileage: [{validator: validatNumber('行驶里程'), trigger: ['change', 'blur']}],
        engineNumber: [{validator: validatString(30), trigger: ['change', 'blur']}],
        frameNumber: [{validator: validatString(30), trigger: ['change', 'blur']}],
        manufacturer: [{validator: validatString(30), trigger: ['change', 'blur']}],
        dateOfProduction: [],
        dateAnnualInspectionExpiration: [],
        dateExpirationOfInsurance: [],
        emissionLevel: [{validator: validatString(30), trigger: ['change', 'blur']}],
        status: [
            {required: true, message: '车辆状态必填项', trigger: ['change', 'blur']},
            {validator: validatString(30), trigger: ['change', 'blur']}
        ],
        ownership: [
            {required: true, message: '车辆归属必填项', trigger: ['change', 'blur']},
            {validator: validatString(30), trigger: ['change', 'blur']}
        ],
        remark: [{validator: validatString(200), trigger: ['change', 'blur']}]
    };

    return RULES;
}
